package com.bolingcavalry.crm.customer.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.customer.service.CustomerCompaniesService;
import com.dto.CustomerCompaniesDTO;
import com.vo.CustomerCompaniesVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "客户管理")
@RestController
@RequestMapping("/companies")
public class CustomerCompaniesController {
    @Autowired
    private CustomerCompaniesService customerCompaniesService;
    //新增客户
    @PostMapping("/add")
    public ResponseResult add(@RequestBody CustomerCompaniesDTO customerCompaniesDTO){
        return customerCompaniesService.add(customerCompaniesDTO);
    }

        //删除客户直接删除
        @DeleteMapping("/delete")
        public ResponseResult delete(@RequestBody List<Long> ids){
            return customerCompaniesService.batchDelete(ids);
        }

    //查询客户
    @GetMapping("/get/{id}")
    public ResponseResult<CustomerCompaniesVO> get(@PathVariable Long id){
        return ResponseResult.success(customerCompaniesService.get(id));
    }

    //修改客户
    @PostMapping("/update")
    public ResponseResult update(@RequestBody CustomerCompaniesDTO customerCompaniesDTO){
        if(customerCompaniesService.update(customerCompaniesDTO)){
            return ResponseResult.success("修改客户"+customerCompaniesDTO.getCompanyName()+"成功");
        }
        return ResponseResult.error("修改客户"+customerCompaniesDTO.getCompanyName()+"失败");
    }

    /**
     * 分页查询客户列表，支持按公司名称和电话模糊搜索
     */
    @GetMapping("/list")
    public ResponseResult<PageResult<CustomerCompaniesVO>> list(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String companyName,
            @RequestParam(required = false) String address,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(required = false) Integer pageSize) {

        Page<CustomerCompaniesVO> page = customerCompaniesService.getByConditions(keyword,companyName, address, pageNum, pageSize);

        PageResult<CustomerCompaniesVO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setPages((int) page.getPages());
        result.setCurrent((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setRecords(page.getRecords());

        return ResponseResult.success("查询成功", result);
    }
}
